﻿using System;
using System.ComponentModel;

namespace Crossroad.Utils.Extensions
{
    /// <summary>
    /// 枚举类型扩展
    /// </summary>
    public static class EnumExt
    {
        /// <summary>
        /// 获取枚举值 string
        /// 编码：陈冉
        /// 2016-6-20 14:43:44
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ValStr(this Enum input)
        {
            return input.GetHashCode().ToString();
        }

        /// <summary>
        /// 获取枚举值 int16
        /// 编码：陈冉
        /// 2019-8-6 11:06:11
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static short ValShort(this Enum input)
        {
            return input.GetHashCode().ToShort();
        }

        /// <summary>
        /// 获取枚举值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int Val(this Enum input)
        {
            return input.GetHashCode();
        }

        /// <summary>
        /// 获取枚举描述文字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string Des(this Enum input)
        {
            var type = input.GetType();
            var fd = type.GetField(input.ToString());
            if (fd == null)
                return string.Empty;

            var attrs = fd.GetCustomAttributes(typeof(DescriptionAttribute), false);
            var des = string.Empty;
            foreach (DescriptionAttribute attr in attrs)
            {
                des = attr.Description;
            }

            return des;
        }


    }
}
